import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;




public class DB_Verwalter 
{

	
	private static PreparedStatement pstmt = null;
	private static Statement stmt = null;
	private static ResultSet rset;
	
	String connect_String;
	String connect_username;
	String connect_password;
	

	public Connection conn()
	{
		Connection db_verbindung;
		IniReader.readIni("ADV");
		
		connect_String  	= IniReader.UMGEBUNG_CONNECT_STRING;
		connect_username 	= IniReader.UMGEBUNG_USERNAME;
		connect_password 	= IniReader.UMGEBUNG_PASSWORD;
		
		
		
		try
		{
			DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
			db_verbindung = DriverManager.getConnection(connect_String, connect_username, connect_password);
			
		}
		catch (Exception e)
		{
			throw new IllegalArgumentException("Fehler bei Verbindungsaufbau");
		}
		
		return db_verbindung;
	}
	
	public void save(Object pObj)
	{
		String sObj;
		String sql_statement = "";
		StringBuffer sBuffer = new StringBuffer();
		Field[] t_Field = pObj.getClass().getFields();
		sObj = pObj.getClass().getSimpleName();
		sBuffer.append("INSERT INTO t_" + sObj + " VALUES ( '");
		
		for (int i = 0; i<t_Field.length; i++)
		{
			try 
			{
				sBuffer.append(t_Field[i].get(pObj) + "' , '");
			} 
			catch (IllegalArgumentException e) 
			{
				
			}
			catch (IllegalAccessException e) 
			{				
				
			}
		}
		
		
		try 
		{
			sBuffer.delete(sBuffer.length()-5, sBuffer.length());
			sBuffer.append("')");
			System.out.println(sBuffer.toString());
			stmt = conn().createStatement();
			stmt.executeUpdate(sBuffer.toString());
		}
		catch (SQLException e) 
		{			
			e.printStackTrace();
		}

	}
	public void executeSkript()
	{
		try 
		{
			stmt = conn().createStatement();
			stmt.executeUpdate("start C:/Users/Niko/workspace/ST_Praktikum5/src/entities/SQLSkriptCBT.txt" );
		}
		catch (SQLException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public Vector getObjectsFrom(String ss_className) 
	{
		Vector ss_vObjects = new Vector();
		
		try 
		{
			
			String sql_statement = "SELECT * FROM t_" + ss_className;
			Class descriptor = Class.forName(ss_className);				
			Field [] t_Field = descriptor.getFields();
			stmt = conn().createStatement();
			ResultSet rset = stmt.executeQuery(sql_statement);
						
			
			while (rset.next())
			{
				
				Object akt_Objekt = descriptor.newInstance();
				
				for (int i = 0; i<t_Field.length; i++)
				{
					Class fieldType = t_Field[i].getType();
					if (fieldType.toString().equals(String.class.toString()))
						t_Field[i].set(akt_Objekt, rset.getString(i+1));
					if (fieldType.toString().equals(int.class.toString()))
						t_Field[i].set(akt_Objekt, rset.getInt(i+1));
				}
				
				ss_vObjects.add(akt_Objekt);
			}
		}
		catch (SQLException e) 
		{
			
		}
		catch (ClassNotFoundException e)
		{
			
		}
		catch (InstantiationException e)
		{
			
		}
		catch (IllegalAccessException e)
		{
			
		}
		

		
		return ss_vObjects;
	}
}
